package com.s4j.core.manager.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONValue;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.Action;
import com.s4j.core.common.BaseAction;
import com.s4j.core.common.StringUtils;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.manager.model.SysModule;
import com.s4j.core.manager.model.SysPerson;
import com.s4j.core.manager.model.SysUser;
import com.s4j.core.manager.service.SysPersonService;
import com.s4j.core.manager.service.SysUserService;

@Controller("com.s4j.core.manager.action.SysUserAction")
@Scope("prototype")
public class SysUserAction extends BaseAction {

	@Resource
	private SysUserService sysUserService;
	
	@Resource
	private SysPersonService sysPersonService;
	
	private SysUser user;
	
	public String query_main() {
		
		DatagridPaging datagridPaging = sysUserService.findUserByPaging(getPage(), getRows(), user);
		
		this.setDatagridPaging(datagridPaging);
		
		return Action.SUCCESS;
	}
	
	public String user_add() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String personId = request.getParameter("personId");
		
		SysPerson person = sysPersonService.get(new Long(personId));
		
		user.setPerson(person);
		
		sysUserService.insert(user);
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String user_edit() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String personId = request.getParameter("personId");
		
		SysPerson person = sysPersonService.get(new Long(personId));
		
		user.setPerson(person);
		
		sysUserService.update(user);
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "修改成功");
		
		return Action.SUCCESS;
	}
	
	public String user_remove() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String userIds = request.getParameter("userIds");
		
		if(StringUtils.isNullOrBlank(userIds)) {
			this.getResultMap().put("rtnCode", Action.SUCCESS);
			this.getResultMap().put("rtnMsg", "请选择要删除的记录");
			return Action.SUCCESS;
		}
		
		sysUserService.remove(userIds);
		
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "操作成功");
		
		return Action.SUCCESS;
	}
	
	public String query_personSnsDialog() {
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		DatagridPaging datagridPaging = sysPersonService.findByPaging(getPage(), getRows(), condition);
		
		this.setDatagridPaging(datagridPaging);
		
		return Action.SUCCESS;
	}
	
	public void user_authTree() {
		
		if(user == null || user.getUserId() == null) {
			return;
		}
		
		List<Map<String, Object>> tree = sysUserService.authTree(user.getUserId(), 1L);
		
		System.out.println(JSONValue.toJSONString(tree));
		
		responseJson(ServletActionContext.getResponse(), JSONValue.toJSONString(tree));
	}
	
	public void user_saveAuth() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String resIds = request.getParameter("resIds");
		
		if(user == null || user.getUserId() == null) {
			return;
		}
		
		sysUserService.saveAuth(user, resIds);
		
		responseJson(ServletActionContext.getResponse(), "操作成功");
	}
	
	public void init(SysModule module ,HttpServletRequest request) {
		Long userId = new Long(request.getParameter("user.userId"));
		request.setAttribute("user",sysUserService.getUser(userId));
	}

	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}
	
}
